﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace CMTVision.Widgets.Impls
{
    public class Widget:System.Web.UI.UserControl, CMTVision.Widgets.Common.IWidget
    {
        #region Implement IWidget Interface

        public string WidgetId
        {
            get
            {
                if (ViewState["WidgetTypeId"] == null)
                    return null;
                return ViewState["WidgetTypeId"].ToString();
            }
            set
            {
                ViewState["WidgetTypeId"] = value;
            }
        }

        public bool IsInEditting
        {
            get
            {
                if (ViewState["IsInEditting"] == null)
                    return false;
                return Convert.ToBoolean(ViewState["IsInEditting"]);
            }
            set
            {
                ViewState["IsInEditting"] = value;
            }
        }


        public Common.IWidgetContainer WidgetContainer
        {
            get;
            set;
        }

        public virtual void Edit()
        {
            throw new NotImplementedException();
        }

        public virtual void Save()
        {
            throw new NotImplementedException();
        }

        public virtual void ForceRefresh()
        {
            throw new NotImplementedException();
        }


        public void Delete()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("~/Data/templatedata.xml"));
            string xPath = "/templatedatas/templatedata/columns/column/widgets/widget[@id='" + WidgetId + "']";
            XmlNode node = xmlDoc.SelectSingleNode(xPath);
            if (node != null)
            {
                node.ParentNode.RemoveChild(node);
                xmlDoc.Save(Server.MapPath("~/Data/templatedata.xml"));
            }
        }

        #endregion

        public override System.Web.UI.Control FindControl(string id)
        {
            return base.FindControl(id);
        }
      
    }
}